<?php

final class JsonResult
{

    const SUCCESS_CODE = 200; //响应成功状态码
    const FAIL_CODE = 400; //响应失败状态码
    const DEFAULT_SUCCESS_MESSAGE = "success";
    const DEFAULT_FAIL_MESSAGE = "fail";

    const LOGIN_SUCCESS_MESSAGE = "用户登录成功！";
    const LOGIN_FAIL_MESSAGE = "用户名或者密码错误！";

    const VERIFICATION_CODE_SUCCESS_MESSAGE = "验证码正确";
    const VERIFICATION_CODE_FAIL_MESSAGE = "验证码错误";

    const TOKEN_SUCCESS_MESSAGE = "TOKEN验证成功！";
    const TOKEN_FAIL_MESSAGE = "TOKEN验证失败！";

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    //用户登录
    public static function login2Json($token = null)
    {
        //如果登录成功则返回值》0
        if (!empty($token)) {
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::LOGIN_SUCCESS_MESSAGE,
                'token' => $token
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } else {
            $data = array(
                'code' => self::FAIL_CODE,
                'msg' => self::LOGIN_FAIL_MESSAGE
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }


    }

    //验证码
    public static function verificationCode($boolValue = false)
    {
        //如果登录成功则返回值》0
        if ($boolValue) {
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::VERIFICATION_CODE_SUCCESS_MESSAGE
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } else {
            $data = array(
                'code' => self::FAIL_CODE,
                'msg' => self::VERIFICATION_CODE_FAIL_MESSAGE
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }


    }

    //token验证
    public static function token2Json($token, $user_id)
    {

        if (!empty($token)) {
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::TOKEN_SUCCESS_MESSAGE,
                'token' => $token,
                'user_id' => $user_id

            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } else {
            $data = array(
                'code' => self::FAIL_CODE,
                'msg' => self::TOKEN_FAIL_MESSAGE

            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }
    }


    //数据库转json
    //$total查询总数, $arr查询的数据, $row修改成功行数
    public static function DB2Json($total, $arr, $row, $method)
    {

        //s则是查询，u增加或修改
        if ($method == 's') {
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::DEFAULT_SUCCESS_MESSAGE,
                'count' => $total,
                'data' => $arr
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } elseif ($method == 'u') {
            //否则是查询
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::DEFAULT_SUCCESS_MESSAGE,
                'update_row' => $row
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } elseif ($method == 'd') {
            //否则是查询
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::DEFAULT_SUCCESS_MESSAGE,
                'delete_row' => $row
            );
            return json_encode($data, JSON_UNESCAPED_UNICODE);
        } elseif ($method == 'a') {
            //否则是查询
            $data = array(
                'code' => self::SUCCESS_CODE,
                'msg' => self::DEFAULT_SUCCESS_MESSAGE,
                'add_row' => $row
            );

            return json_encode($data, JSON_UNESCAPED_UNICODE);
        }

    }

    /**
     * @param $total_page 总页数
     * @param $current_page 当前页
     * @param $arr 分页数据
     * @param $method 查询方法
     * @return void
     */
    public static function customQuery2Json($total_records, $arr)
    {
        $data = array(
            'code' => self::SUCCESS_CODE,
            'msg' => self::DEFAULT_SUCCESS_MESSAGE,
            "total" => $total_records,
            "data" => $arr
        );

        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    public static function customDaoQuery2Json($total, $arr){
        $data = array(
            'code' => self::SUCCESS_CODE,
            'msg' => self::DEFAULT_SUCCESS_MESSAGE,
            "total" => $total,
            "data" => $arr
        );

        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    //json转数组,去除code
    public static function json2analysis($jsonData)
    {
        if (!empty($jsonData)) {
            $json = json_decode($jsonData);
            $obj = $json->data;

            if (count($obj) > 0) {
                $json_data = json_encode($obj);
                $arr = json_decode($json_data, true);   // 将JSON数据格式转换为PHP数组（注意第二个参数true）

                return $arr;

            }
        }

    }

    //通过jsonData获得总数
    public static function getCountByJsonData($jsonData)
    {
        if (!empty($jsonData)) {
            $json = json_decode($jsonData);
            $total = $json->count;

            return $total;
        }

    }

    public static function getValue($data,$paramet)
    {
        if (!empty($data)) {
            $json = json_decode($data);
            $val = $json->$paramet;

            return $val;
        }

    }


    //数组添加code返回
    public static function arr2Restful($arrData, $total)
    {
        $data = array(
            'code' => self::SUCCESS_CODE,
            'msg' => self::DEFAULT_SUCCESS_MESSAGE,
            'total' => $total,
            'data' => $arrData
        );
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }

    //数组转json，没有code
    public static function arrJson($data)
    {
        return json_encode($data, JSON_UNESCAPED_UNICODE);
    }
}


?>